package com.pkk.penguin.frame.know.boot.view.controller.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import com.pkk.penguin.frame.know.boot.config.common.handle.AbstractWebSocketServer;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @description:
 * @author: peikunkun
 * @create: 2020-06-01 13:56
 **/
@Slf4j
public abstract class AbstractAppletController<S> {

  /**
   * @Description: 获取平台信息
   * @return: com.pkk.components.rpc.response.CommonResponse
   * @Author: peikunkun
   * @Date: 2020/5/22 0022 下午 4:00
   */
  @RequestMapping("platform")
  protected CommonResponse<JSONArray> platform() {
    JSONArray platform = this.handlePlatformList();
    return ResponseUtil.success(platform);
  }


  /**
   * @Description: 是否连接的
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/5/13 0013 下午 4:02
   */
  @RequestMapping("connected")
  public CommonResponse<Boolean> connected(@RequestBody CommonRequest<JSONObject> request) {
    final JSONObject body = request.getBody();
    final String clientCode = body.getString("clientCode");
    if (StringUtils.isBlank(clientCode)) {
      return ResponseUtil.success(false);
    }
    ConcurrentHashMap<String, AbstractWebSocketServer> webSocket = this.getWebSocketServer();
    return ResponseUtil.success(webSocket.containsKey(clientCode));
  }

  /**
   * @Description: 获取webSocketServer
   * @Param:
   * @return: java.util.concurrent.ConcurrentHashMap<java.lang.String, com.pkk.penguin.frame.know.boot.config.common.handle.AbstractWebSocketServer>
   * @Author: peikunkun
   * @Date: 2020/6/1 0001 下午 2:07
   */
  protected abstract ConcurrentHashMap<String, AbstractWebSocketServer> getWebSocketServer();


  /**
   * @Description: 处理平台列表
   * @Param:
   * @return: com.alibaba.fastjson.JSONArray
   * @Author: peikunkun
   * @Date: 2020/6/1 0001 下午 2:04
   */
  protected abstract JSONArray handlePlatformList();

}
